#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<vector>

using namespace std;

#define NUM 5
int ticket = 100;


class threadData
{
public:
	threadData(int number){
		threadname = "thread-" + to_string(number);
	}
				
public:
	string threadname;
};

void* getTicket(void* args)
{
	threadData *td = static_cast<threadData*>(args);		    
	while(1)
	{
		if(ticket > 0)
		{
			usleep(1000);     // 模拟抢票的时间
			 printf("%s is running, get a ticket: %d\n", td->threadname.c_str(), ticket);
			ticket--;
			}
		else{
         break;
        }
	}
	return nullptr;
}


int main(){
	 vector<pthread_t> tids;
	 vector<threadData*> thread_datas;

	 for(int i = 1; i <= NUM; i++){
	    threadData* td = new threadData(i);
	    pthread_t tid;
		pthread_create(&tid, nullptr, getTicket, td);
		tids.push_back(tid);
		thread_datas.push_back(td);
	}
				    
	for(auto e : tids){
      pthread_join(e, nullptr);
    }		

	for(auto td : thread_datas){
	  delete td;
    }

	return 0;
}